﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
// ColorPicker, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace ColorPicker
{
    /// <summary>
    /// Interaction logic for UCbutton.xaml
    /// </summary>
    public partial class UCbutton : UserControl
    {
        private readonly Thickness BORDER_THICKNESS_DEFAULT = new Thickness(1);
        private readonly Thickness BORDER_THICKNESS_MARKED = new Thickness(5);
        //
        private readonly TimeSpan TIMER_DELAY_DEFAULT = new TimeSpan(0, 0, 0, 2);
        private readonly TimeSpan TIMER_DELAY_LONG = new TimeSpan(0, 0, 0, 20);
        //
        //
        private System.Windows.Threading.DispatcherTimer dt = null;
        public UCbutton()
        {
            InitializeComponent();
            //
        }
        //
        private ColorsCooked _EssentialColor = null;
        public ColorsCooked EssentialColor
        {
            get
            {
                return _EssentialColor;
            }
            set
            {
                _EssentialColor = value;
                grid1.Background = new SolidColorBrush(Color.FromRgb(value.ColorR, value.ColorG, value.ColorB));
                //
                borderOuter.BorderBrush = Brushes.Black;
                //
                textInternal.Foreground = new SolidColorBrush(Color.FromRgb((byte)(255 - value.ColorR), (byte)(255 - value.ColorG), (byte)(255 - value.ColorB)));
                textInternal.Background = grid1.Background;
                //
                if (0 == value.ColorR && 0 == value.ColorG && 0 == value.ColorB)
                {
                    textInternal.Text = "";
                }
                else if (255 == value.ColorR && 255 == value.ColorG && 255 == value.ColorB)
                {
                    textInternal.Text = "•";
                }
                else if (255 == value.ColorR && 0 == value.ColorG && 0 == value.ColorB)
                {
                    textInternal.Text = "★";
                }
                else if (0 == value.ColorR && 255 == value.ColorG && 0 == value.ColorB)
                {
                    textInternal.Text = "★";
                }
                else if (0 == value.ColorR && 0 == value.ColorG && 255 == value.ColorB)
                {
                    textInternal.Text = "★";
                }
                //
                //
            }
        }
        //
        public bool IsLongDelay
        {
            get;
            set;
        }
        //
        private void grid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            borderOuter.BorderThickness = BORDER_THICKNESS_MARKED;
            //
            if(null == dt)
            {
                dt = new System.Windows.Threading.DispatcherTimer();
                dt.IsEnabled = false;
                dt.Tick += tickHandler;
            }
            //
            if (IsLongDelay)
            {
                dt.Interval = TIMER_DELAY_LONG;
            }
            else
            {
                dt.Interval = TIMER_DELAY_DEFAULT;
            }
            dt.IsEnabled = true;
            //
            e.Handled = true;
        }
        //
        //
        private void tickHandler(object sender, EventArgs e)
        {
            dt.IsEnabled = false;
            if (borderOuter.BorderThickness == BORDER_THICKNESS_DEFAULT) return;


            if (grid1.CheckAccess())
            {
                borderOuter.BorderThickness = BORDER_THICKNESS_DEFAULT;
            }
            else
            {
                grid1.Dispatcher.BeginInvoke((Action)(() =>
                {
                    borderOuter.BorderThickness = BORDER_THICKNESS_DEFAULT;
                }));
            }
        }
        //
    }
}
